//下拉框
$('.select2').select2();
//日期  
$('#reservation').daterangepicker({
	startDate: '2017-10-10',
	locale: {
		format: 'YYYY-MM-DD'
	}
});
$('#reservation').val('请选择时间');
//搜索
$('.search_order').click(function() {
	getData1(1);
})

var orderN = '';//订单编码
var orderM = '';//订单id
//初始化
getData1(1);
//分页
function pageDivd(curr, page_num) {
	layui.use(['laypage', 'layer'], function() {
		var laypage = layui.laypage,
			layer = layui.layer;
		laypage.render({
			elem: 'demo7',
			count: page_num,
			limit: 20,
			curr: curr,
			jump: function(obj, first) {
				if(!first) {
					getData1(obj.curr);

				}
			}
		});
	})
}
//列表渲染
function TableView(data) {
	$('.table_data').html('');

	for(var i = 0; i < data.length; i++) {
		var operateBox;
		var labelText;
		if(data[i][5] == '待发货') {
			labelText = `<label class="label1">${data[i][5]}</label>`;
			operateBox = `<div class="btn_done">
		      		    				<a class="order_send order_btn" index="1" href="#">订单发货</a>
		      		    				<a class="order_detail" href="#">订单详情</a>
		      		    			</div>`;

		} else if(data[i][5] == '已归还') {
			labelText = `<label class="label1">${data[i][5]}</label>`;
			operateBox = `<div class="btn_done">
		      		    				<a class="order_btn" index="2" href="#">确认归还</a>
		      		    				<a class="order_detail" href="#">订单详情</a>
		      		    			</div>`;

		} else if(data[i][5] == '已取消') {
			labelText = `<label class="label1">${data[i][5]}</label>`;
			operateBox = `<div class="btn_done">
		      		    				<a class="order_btn" index="3" href="#">关闭订单</a>
		      		    				<a class="order_detail" href="#">订单详情</a>
		      		    			</div>`;
		} else {
			labelText = `<label class="">${data[i][5]}</label>`;
			operateBox = `<div class="btn_done">
		      		    				<a class="order_detail" href="#">订单详情</a>
		      		    			</div>`;
		}

		var trText = `<tr num="${data[i][0]}">
		      		    		<td>${data[i][0]}</td>
		      		    		<td>${data[i][1]}</td>
		      		    		<td> 
		      		    			${data[i][2][0]}
		      		    		</td>
		      		    		<td>
		      		    			${data[i][3]}
		      		    		</td>
		      		    		<td>
		      		    			${data[i][4]}
		      		    		</td>
		      		    		<td>
		      		    			${labelText}
		      		    		</td>
		      		    		<td>
		      		    			${operateBox}
		      		    		</td>
		      		    	</tr>
		      		    	`;

		$('.table_data').append(trText);

	}
	look_detail();
}
//查看详情
function look_detail() {
	$('.order_detail').click(function() {
		var Index = $(this).parents('tr').attr('num');
		window.location.href = "http://119.23.212.200:30000/order_detail.html?num=" + Index;

	})

	$('.order_send').click(function() {
		$('.order_form').css('display', 'block');
		$('.close_btn').click(function() {
			$('.order_form').css('display', 'none');
		})

	})

	//订单发货
	$('.order_save').click(function() {
		var sendBus = $('.send_text').select2('val');
		$.ajax({
			type: "get",
			url: "https://www.retux.cn/MgOrdersControl",
			async: true,
			data: {
				Id: orderM,
				Kind: orderN,
				Company: sendBus,
				No: $('.send_num').val(),
				UserId: $.cookie('UserId')
			},
			success: function(data) {
				if(data == 'success') {
					layer.msg('提交成功', {
						time: 500
					}, function() {
						location.reload();
						layer.closeAll();
					});

				} else {
					layer.msg('提交失败')
				}
			}
		});
	})
	$('.order_btn').click(function() {
		
		var index = $(this).attr('index');
		var orderNum = $(this).parents('tr').attr('num');
		
		//订单发货的参数
		orderN = $(this).attr('index');
		orderM = $(this).parents('tr').attr('num');
		if(index != 1) {
			$.ajax({
				type: "get",
				url: "https://www.retux.cn/MgOrdersControl",
				async: true,
				data: {
					Id: orderNum,
					Kind: index,
					Company: $('.send_text').val(),
					No: $('.send_num').val(),
					UserId: $.cookie('UserId')
				},
				success: function(data) {
					if(data == 'success') {
						layer.msg('提交成功',{time:500},function(){
							location.reload();
						})
					} else {
						layer.msg('提交失败')
					}
				}
			});
		}

	})

}

function getData1(curr) {
	var data_rang = $('#reservation').val(); //时间范围
	var evKind = $('.event_kind').select2('val');
	var start = data_rang.split(' -')[0];
	var end = data_rang.split('- ')[1];
	var timeOnoff = "False";
	if(data_rang != '' && data_rang != '请选择时间') {
		timeOnoff = "True"
	}
	$.ajax({
		type: "get",
		url: "https://www.retux.cn/MgOrdersList",
		async: true,
		data: {
			page: curr,
			row: 20,
			Product: $('.name_text').val(),
			No: $('.num_text').val(),
			end: end,
			Kind: $('.event_kind').select2('val'),
			start: start,
			Time: timeOnoff
		},
		success: function(data) {
			if(data[0].length == 0) {
				layer.msg('暂无数据')
			}
			TableView(data[0]);
			pageDivd(curr, data[1]);
			$('.all_data').html(data[1])
		}
	});
}